package com.chinasoft.goodsManager.goodslistserver.impl;

import com.chinasoft.goodsManager.dao.impl.GoodsListDaoImpl;
import com.chinasoft.goodsManager.daomaingoodslist.Goods;
import com.chinasoft.goodsManager.daomaingoodslist.UserList;
import com.chinasoft.goodsManager.goodslistserver.ListService;

import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ListServiceImpl implements ListService {

    static GoodsListDaoImpl goodsDao = new GoodsListDaoImpl();

    //查询数据库返回值
    @Override
    public List<Goods> query() {
        return goodsDao.query();
    }

    //删除带有id的行
    @Override
    public void goodsselete(Integer id) {
        goodsDao.goodsselete(id);
    }

    //userlist页面查询数据
    @Override
    public List<UserList> querys() {return goodsDao.querys();}

    //删除userlist行
    @Override
    public void userselete(Integer id) {
        goodsDao.userSelete(id);
    }


    @Override
    public Object [] detailed( Integer id ) {return goodsDao.detailed(id);}

    @Override
    public void add(UserList userList) {
        goodsDao.add(userList);
    }

    @Override
    public Object [] modift(String input) {
        int a=0;
        Integer integer = Integer.valueOf(input);
        List<Map<String, Object>> list = goodsDao.modift(integer);
        String[] arr = new String[12];
        for (Map<String, Object> map : list) {
            Set<String> key = map.keySet();
            for (String s : key) {
                arr[a] = String.valueOf(map.get(s));
                a++;
            }
        }
        try {
            FileReader txt = new FileReader("D:\\GoodManagerFile\\PersonFile\\"+arr[11]);
            int len;
            String str="";
            while ((len = txt.read()) != -1){
                str = str + (char)len;
            }
            arr[11]=str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arr;
    }




    @Override
    public void update(UserList userList) {
        //获取没有修改前的图片和简介
        Object[] detailed = goodsDao.detailed(userList.getId());
        //判断是否有新图片
        System.out.println(detailed[4]);
        if(userList.getPimg()==null||userList.getPimg().equals("")){
            userList.setPimg(String.valueOf(detailed[3]));
        }else {
            //删除图片
            File fileoldimg = new File("D:\\GoodManagerFile\\PersonImg\\"+detailed[3]);
            fileoldimg.delete();
        }
        File fileolddescrike = new File("D:\\GoodManagerFile\\PersonFile\\"+detailed[4]);
        //删除简介
        boolean delete = fileolddescrike.delete();
        System.out.println(delete);
        goodsDao.update(userList);
    }

    @Override
    public Object[] person(Integer id) {

        return goodsDao.person(id);
    }


}
